package dryven.sapi.servlet_v25;

import java.io.File;

import javax.servlet.ServletContext;

import dryven.config.ApplicationConfiguration;
import dryven.config.ConfigurationException;
import dryven.config.DatabaseConfiguration;
import dryven.config.MailSenderConfiguration;
import dryven.init.FrontControllerFactory;
import dryven.util.ArrayIterable;
import dryven.util.crypt.RandomAlphaNumericalString;

public class ServletApplicationConfiguration implements
		ApplicationConfiguration {
	
	private ServletContext ctx;
	private ApplicationConfiguration delegateConfig;
	private final String SIGNSECRETKEY = "dryven-sign-secret-word";

	public ServletApplicationConfiguration(ServletContext ctx, ApplicationConfiguration delegateConfig) {
		super();
		this.ctx = ctx;
		this.delegateConfig = delegateConfig;
	}

	@Override
	public String getApplicationRootPath() {
		return ctx.getRealPath("/");
	}

	@Override
	public String getApplicationRootURLPath() {
		String delegateURL = delegateConfig.getApplicationRootURLPath();
		if(delegateURL!=null) {
			return delegateURL;
		}
		return ctx.getContextPath();
	}
	
	@Override
	public String getSignSecret() {
		String signsecret = (String)ctx.getAttribute(SIGNSECRETKEY);
		if(signsecret==null) {
			signsecret = new RandomAlphaNumericalString(128, false).generate();
			ctx.setAttribute(SIGNSECRETKEY, signsecret);
		}
		return signsecret;
	}

	public Class<? extends FrontControllerFactory> getFrontControllerFactoryClass() {
		return delegateConfig.getFrontControllerFactoryClass();
	}

	public Iterable<String> getModelPackages() {
		return delegateConfig.getModelPackages();
	}

	public Iterable<String> getControllerPackages() {
		return delegateConfig.getControllerPackages();
	}

	private String combine(String[] s, String glue) {
		int k=s.length;
		if (k==0) {
			return null;
		}
		StringBuilder out=new StringBuilder();
		out.append(s[0]);
		for (int x=1;x<k;++x) {
			out.append(glue).append(s[x]);
		}
		return out.toString();
	}
	
	public String getCustomConfigValue(String... keys) {
		String key = combine(keys, ".");
		return ctx.getInitParameter(key);
	}

	public DatabaseConfiguration getDatabaseConfigurationByName(String connName)
			throws ConfigurationException {
		return delegateConfig.getDatabaseConfigurationByName(connName);
	}

	public Iterable<DatabaseConfiguration> getDatabaseConfigurations() {
		return delegateConfig.getDatabaseConfigurations();
	}

	public DebugEnvironment getDebugEnvironment() {
		return delegateConfig.getDebugEnvironment();
	}

	public DatabaseConfiguration getDefaultDatabaseConfiguration() {
		return delegateConfig.getDefaultDatabaseConfiguration();
	}

	public MailSenderConfiguration getMailSenderConfiguration() {
		return delegateConfig.getMailSenderConfiguration();
	}

	public String getServerName() {
		return delegateConfig.getServerName();
	}

	public File getUserDataDirectory() {
		return delegateConfig.getUserDataDirectory();
	}

	@Override
	public Iterable<String> getViewPackages() {
		return delegateConfig.getViewPackages();
	}

	@Override
	public Iterable<String> getClassPathEntries() {
		return new ArrayIterable<String>(new String[]{
			ctx.getRealPath("/WEB-INF/classes/")
		});
	}
	
	
}
